package com.guigu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.util.StringUtil;
import com.guigu.mapper.*;

import com.guigu.mapper.OrdersParticularsMapper;
import com.guigu.mapper.ShopInfoMapper;
import com.guigu.mapper.UsersMapper;
import com.guigu.pojo.*;
import com.guigu.service.CompanyRunningWaterService;
import com.guigu.service.OrdersParticularsService;
import com.guigu.service.OrdersService;
import com.guigu.service.utils.MyIdAdd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.SimpleFormatter;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class CompanyRunningWaterServiceImpl extends ServiceImpl<CompanyRunningWaterMapper, CompanyRunningWater>
        implements CompanyRunningWaterService {

    @Autowired
    ShopInfoMapper shopMapper;// 商品Mapper;

    @Autowired
    MyIdAdd myIdAdd; //编号工具

    @Autowired
    CompanyRunningWaterMapper companyRunningWaterMapper;

    @Autowired
    UsersMapper usersMapper;

    @Autowired
    OrdersMapper ordersMapper;//订单

    @Autowired
    OrdersService ordersService;//订单

    //商户根据页面选择的时间去查询  营收
    @Override
    public List<CompanyRunningWater> queryAllys(CompanyRunningWater companyRunningWater,Integer loginUserId) {
        QueryWrapper<CompanyRunningWater> queryWrapper=new QueryWrapper<CompanyRunningWater>();
        if(loginUserId!=null){
            queryWrapper.eq("uid",loginUserId);
            queryWrapper.eq("rtype","3");
            queryWrapper.eq("is_delete","0");
        }
        if(companyRunningWater.getStartTime()!=null){
            queryWrapper.ge("rdate",companyRunningWater.getStartTime());
        }
        if(companyRunningWater.getEndTime()!=null){
            queryWrapper.le("rdate",companyRunningWater.getEndTime());
        }
        queryWrapper.orderByAsc("rdate");
        return this.list(queryWrapper);
    }

    /**
     * 后台营收
     * @param water
     * @return
     */
    @Override
    public List<CompanyRunningWater> queryWaters(CompanyRunningWater water) {
        QueryWrapper<CompanyRunningWater> queryWrapper=
                new QueryWrapper<CompanyRunningWater>();
        queryWrapper.eq("is_delete",0);
        if (water.getRtype()!=null){
            queryWrapper.eq("rtype",water.getRtype());
        }
        if (water.getUid()!=null){
            queryWrapper.eq("uid",water.getUid());
        }
        if(water.getStartTime()!=null){
            queryWrapper.ge("rdate",water.getStartTime());
        }
        if(water.getEndTime()!=null){
            queryWrapper.le("rdate",water.getEndTime());
        }
        return this.list(queryWrapper);
    }

    //后台统计每月商品销售量
    @Override
    public List<ShopInfo> queryBuyNum(CompanyRunningWater water) {
        return  shopMapper.queryBuyNum(water);
    }

    //根据订单id查询流水记录
    @Override
    public CompanyRunningWater queryWaterByOid(Integer oid) {
        QueryWrapper<CompanyRunningWater> queryWrapper=
                new QueryWrapper<CompanyRunningWater>();
        queryWrapper.eq("oid",oid);
        return this.getOne(queryWrapper);
    }

    //添加流水
    @Override
    public Map addWater(CompanyRunningWater water) {
        Map map=new HashMap();
        map.put("code",0);
        map.put("msg","添加失败!");
        water.setPipelineId(myIdAdd.idadd("500",true));
        boolean save = this.save(water);
        if (save){
            map.put("code",1);
            map.put("msg","添加成功!");
        }
        return map;
    }

    @Override
    public List<String> getIds(String str) {
        List<String> list = new ArrayList<String>();
        QueryWrapper<CompanyRunningWater> queryWrapper = new QueryWrapper<CompanyRunningWater>();
        queryWrapper.likeRight("pipeline_id", str);
        List<CompanyRunningWater> tempList = this.list(queryWrapper);
        for (CompanyRunningWater obj : tempList) {
            list.add(obj.getPipelineId());
        }
        return list;
    }

    @Override
    public List<CompanyRunningWater> queryAllgysys(CompanyRunningWater companyRunningWater, Integer loginUserId) {
        QueryWrapper queryWrapper=new QueryWrapper<CompanyRunningWater>();
        if(loginUserId!=null){
            queryWrapper.eq("uid",loginUserId);
            queryWrapper.eq("rtype","4");
            queryWrapper.eq("is_delete","0");
        }
        if(companyRunningWater.getStartTime()!=null){
            queryWrapper.ge("rdate",companyRunningWater.getStartTime());
        }
        if(companyRunningWater.getEndTime()!=null){
            queryWrapper.le("rdate",companyRunningWater.getEndTime());
        }
        queryWrapper.orderByAsc("rdate");

        return this.list(queryWrapper);
    }

    //分页查询
    @Override
    public Page<CompanyRunningWater> queryPage(CompanyRunningWater water, Integer pageno, Integer pagesize) {
        QueryWrapper queryWrapper=
                new QueryWrapper<CompanyRunningWater>();
        //流水编号
        if (StringUtil.isNotEmpty(water.getPipelineId())){
            queryWrapper.eq("pipeline_id",water.getPipelineId());
        }
        //流水类型
        if (water.getRtype()!=null){
            queryWrapper.eq("rtype",water.getRtype());
        }
        //时间查询
        if(water.getStartTime()!=null){
            queryWrapper.ge("rdate",water.getStartTime());
        }
        if(water.getEndTime()!=null){
            queryWrapper.le("rdate",water.getEndTime());
        }
        //删除状态
        if(water.getIsDelete()!=null){
            queryWrapper.eq("is_delete",water.getIsDelete());
        }
        Page<CompanyRunningWater> page = this.page(new Page<CompanyRunningWater>(pageno, pagesize),queryWrapper);
        //补全属性
        for(CompanyRunningWater c : page.getRecords()){
            c.setAccountIdUsers(usersMapper.selectById(c.getAccountId()));
            c.setUidUsers(usersMapper.selectById(c.getUid()));
        }
        return page;
    }

    //商户工资结算
    @Override
    public Map merchantsAndSettlement(Integer id) {
        Map<String,Object> map=new HashMap<>();
        map.put("msg","工资结算成功！");
        map.put("x",true);
        //获取当前时间，在当前时间进行减少一个月，查询
        Calendar date = Calendar.getInstance();
        date.setTime(new Date());//减少一个月的时间
        date.add(Calendar.MONTH, -1);
        QueryWrapper queryWrapper=new QueryWrapper<Orders>();
        //获取年月日进行组合转字符串
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String clinchDate = df.format(date.getTime());
        //设置查询条件
        Orders orders=new Orders();
        //商户id
        orders.setCommId(id);
        //查询一个月前的数据【因为商户工资为月结】
        orders.setClinchDateStr(clinchDate);
        List<Orders> ordersList=ordersMapper.merchantsAndSettlementQuery(orders);;
        //获取订单总数，计算工资，添加到流水表内【这里一以每一个订单为2元计算】
        Double salary=ordersList.size()*0.5;
        //添加一条流水表
        CompanyRunningWater companyRunningWater=new CompanyRunningWater();
        //流水编号
        companyRunningWater.setPipelineId(myIdAdd.idadd("500",true));
        //付钱用户id
        companyRunningWater.setAccountId(10);
        //收钱用户id
        companyRunningWater.setUid(id);
        //账单编号【由于存在多个订单，没有添加详情的情况下，所以就先设置为null】
        companyRunningWater.setOid(null);
        //流水金额
        companyRunningWater.setRmoney(salary);
        //流水日期
        companyRunningWater.setRdate(new Date());
        //流水类型【结算商户月营收】
        companyRunningWater.setRtype(3);
        //删除状态
        companyRunningWater.setIsDelete(0);
        //进行添加
        companyRunningWaterMapper.insert(companyRunningWater);
        //循环订单，将工资结算状态设置为已结算
        for(Orders o : ordersList){
            o.setIsSettleAccounts(1);
            //进行修改
            ordersMapper.updateById(o);
        }
        return map;
    }

    @Override
    public com.guigu.service.utils.Page merchantsAndSettlementQuery(Orders orders, Integer pageno, Integer pagesize) {
        //获取当前时间，在当前时间进行减少一个月，查询
        Calendar date = Calendar.getInstance();
        date.setTime(new Date());//减少一个月的时间
        date.add(Calendar.MONTH, -1);
        QueryWrapper queryWrapper=new QueryWrapper<Orders>();
        //获取年月日进行组合转字符串
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String clinchDate = df.format(date.getTime());
        //设置查询条件
        //查询一个月前的数据【因为商户工资为月结】
        Orders orders2=new Orders();
        //商户id
        orders2.setCommId(orders.getCommId());
        orders2.setClinchDateStr(clinchDate);
        List<Orders> ordersList=ordersMapper.merchantsAndSettlementQuery(orders2);
        com.guigu.service.utils.Page page=new com.guigu.service.utils.Page();
        page=page.pageList(ordersList,pageno,pagesize);
        List<Orders> records=page.getRecords();
        //补齐属性
        for(Orders o : records){
            o.setUsers(usersMapper.selectById(o.getUId()));
        }
        return page;
    }



    @Override
    public Double thisMonthRevenue(Integer id) {
        Calendar data= Calendar.getInstance();
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM");
        String clinchDate=df.format(data.getTime());
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.like("rdate",clinchDate);
        queryWrapper.eq("uid",id);
        queryWrapper.eq("rtype",4);
        List<CompanyRunningWater> c=companyRunningWaterMapper.selectList(queryWrapper);
        Double sum=0d;
        for (CompanyRunningWater l : c){
            sum+=l.getRmoney();
        }
        return sum;
    }
}
